package com.xclink.app.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xclink.app.utils.HttpReq;

/**
 * 客服消息
 * @author ljp
 *
 */
@Service
public class CustomerService {
	@Autowired
	private TokenService tokenService;
//	客服消息接口
	private String url ="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
//	发送客服消息之发送文本消息
	public String sendCustomerServiceMessage(String openId, String content){
		String access_token = tokenService.LocalAccessToken();
		System.out.println("打印access_token====="+access_token);
		JSONObject json = new JSONObject();
		JSONObject json2 = new JSONObject();
		json2.put("content","客服消息："+ content);
		json.put("touser", openId);
		json.put("msgtype", "text");
		json.put("text", json2);
		
		url = url.replace("ACCESS_TOKEN", access_token);
		String s = HttpReq.sendPost(url, json.toString());
		System.out.println("s====" + s);
		Map sMap = JSON.parseObject(s);
		Integer errcode = (Integer)sMap.get("errcode");
		String errmsg = (String)sMap.get("errmsg");
		if(errcode==45015){
			errmsg="回复时间超过限制";
		}else if(errcode==0){
			errmsg="发送成功";
		}else if(errcode==40001){
			errmsg="获取 access_token 时 AppSecret 错误，或者 access_token 无效。请开发者认真比对 AppSecret 的正确性，或查看是否正在为恰当的公众号调用接口";
		}else if(errcode==45047){
			errmsg="客服接口下行条数超过上限（20条）（用户与公众号交互后会重置此限制）";
		}
		return errmsg;
	}
	
	
	
	
	
	
	// 给文本加超链接
		public String addHref(String content, String url) {
			return "<a href=" + '"' + url + '"' + ">" + content + "</a>";

		}
}
